/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/2 11:29 上午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
// import { getChildTreeData } from "utils/index";
import {
    createEvaluateTask,
    getDepartmentTreeList,
    getEvaluateObjectList,
    getExamList,
    getPagedEvaluateTask,
    getTaskUserInfo,
    changeEvaluateTaskInfo,
    getQuestionCategoryList,
    getExpertUserModelList,
} from 'api/index';

export default {
    namespace: 'task',
    state: {
        dataSource: {},
        keyword: '',
        reqPageInfo: {
            pageNumber: 1,
            pageSize: 10,
        },
        modalTitle: '新增评价任务',
        modalVisible: false,
        itemData: {},
        // objectList: [],
        examOptionList: [],
        expertTeacherListOption: [],
        treeData: [],
        finishSampleCountList: [],
        finishSampleCountModalVisible: false,
        finishStatus: '全部',
        evaluateTaskId: '',
        questionCategoryList: [],

    },
    reducers: {
        setDataSource(state, { payload: { dataSource, keyword, reqPageInfo } }) {
            return { ...state, dataSource, keyword, reqPageInfo };
        },
        setModalVisible(state, { payload: modalVisible }) {
            return { ...state, modalVisible };
        },
        setModalTitle(state, { payload: modalTitle }) {
            return { ...state, modalTitle };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        // setObjectList(state, { payload: objectList }) {
        //     return { ...state, objectList };
        // },
        setExamOptionList(state, { payload: examOptionList }) {
            return { ...state, examOptionList };
        },
        setExpertTeacherListOption(state, { payload: expertTeacherListOption }) {
            return { ...state, expertTeacherListOption };
        },
        setUnitsTreeData(state, { payload: treeData }) {
            return { ...state, treeData };
        },
        setFinishSampleCountModalVisible(state, { payload: finishSampleCountModalVisible }) {
            return { ...state, finishSampleCountModalVisible };
        },
        setFinishStatus(state, { payload: finishStatus }) {
            return { ...state, finishStatus };
        },
        setFinishSampleCountList(state, { payload: finishSampleCountList }) {
            return { ...state, finishSampleCountList };
        },
        setEvaluateTaskId(state, { payload: evaluateTaskId }) {
            return { ...state, evaluateTaskId };
        },
        setQuestionCategoryList(state, { payload: questionCategoryList }) {
            return { ...state, questionCategoryList };
        },
    },
    effects: {
        /**
         * @desc 任务列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getPagedEvaluateTask, payload);
            if (result.status === 'success') {
                const dataSource = result.result;
                yield put({
                    type: 'setDataSource',
                    payload: {
                        dataSource,
                        keyword: payload.keyword,
                        reqPageInfo: payload.reqPageInfo,
                    },
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取评定对象
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getObjectList({ payload }, { call, put, select }) {
            const result = yield call(getEvaluateObjectList, payload);
            if (result.status === 'success') {
                const objectList = result.result;
                yield put({
                    type: 'setObjectList',
                    payload: objectList,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取方案浏览
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getExamOptionList({ payload /*callback*/ }, { call, put, select }) {
            const result = yield call(getExamList, payload);
            if (result.status === 'success') {
                const examOptionList = result.result;
                yield put({
                    type: 'setExamOptionList',
                    payload: examOptionList,
                });
                // if (callback && typeof callback === "function") {
                //     callback(examOptionList);
                // }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取督导专家
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getExpertTeacherListOption({ payload }, { call, put, select }) {
            const result = yield call(getExpertUserModelList, payload);
            if (result.status === 'success') {
                const expertTeacherListOption = result.result;
                yield put({
                    type: 'setExpertTeacherListOption',
                    payload: expertTeacherListOption,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取单位
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getUnitsTreeData({ payload }, { call, put, select }) {
            const result = yield call(getDepartmentTreeList, payload);
            if (result.status === 'success') {
                const data = result.result;
                // const treeData = data.map(c => {
                //     if (!c.hasLowerDepartment) {
                //         return {
                //             title: c.departmentName,
                //             key: c.departmentId,
                //             comment: c.comment,
                //             hasLowerDepartment: c.hasLowerDepartment,
                //             level: c.level
                //         };
                //     } else {
                //         return {
                //             title: c.departmentName,
                //             key: c.departmentId,
                //             comment: c.comment,
                //             hasLowerDepartment: c.hasLowerDepartment,
                //             level: c.level,
                //             children: getChildTreeData(c.lowerDepartmentModel)
                //         };
                //     }
                // });
                yield put({
                    type: 'setUnitsTreeData',
                    payload: data,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 创建任务
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addEvaluateTask({ payload, callback }, { call, put, select }) {
            const result = yield call(createEvaluateTask, payload);
            if (result.status === 'success') {
                message.success(`添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 任务完成情况列表
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getTaskUserInfoTableList({ payload, callback }, { call, put, select }) {
            const result = yield call(getTaskUserInfo, payload);
            if (result.status === 'success') {
                const finishSampleCountList = result.result;
                yield put({
                    type: 'setFinishSampleCountList',
                    payload: finishSampleCountList,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 修改任务
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* updateEvaluateTaskInfo({ payload, callback }, { call, put, select }) {
            const result = yield call(changeEvaluateTaskInfo, payload);
            if (result.status === 'success') {
                message.success(`${payload.evaluateTaskName}添加成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        * getQuestionCategoryList({ payload }, { call, put, select }) {
            const result = yield call(getQuestionCategoryList, payload);
            if (result.status === 'success') {
                const questionCategoryList = result.result;
                yield put({
                    type: 'setQuestionCategoryList',
                    payload: questionCategoryList,
                });
            } else {
                message.error(result.message);
            }

        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp('/evaluations/task').exec(pathname)) {

                }
            });
        },
    },
};
